//Modified By Reem Sharawy 3/9/2013
public class icn_ctlr 
{
    Public Health_Card_Renewal_Form__c Application {get; set;}
    Public Boolean show_NewDesignation {get; set;}
    Public Boolean show_NewPassport {get; set;}
    Public Boolean show_ChangeOfNationality {get; set;}
    Public Boolean show_Others {get; set;}
    
    Public String visaIssueDate {get; set;}
    Public String companyDate {get; set;}
    
    
    public icn_ctlr()
    {
        show_NewDesignation = false;
        show_Newpassport = false;
        Application = new Health_Card_Renewal_Form__c();
        Application.First_Name__c = 'First';
        Application.Middle_Name__c = 'Middle';
        Application.Last_Name__c = 'Last';
    }
   /* public void Render_NewPassport()
    {
        if(Application.request_type__c == 'New Passport' && Application.request_type__c != 'New Designation' && Application.request_type__c != 'Change Of Nationality' &&  Application.request_type__c != 'Others' && Application.request_type__c != 'New Passport')
        {
            show_NewDesignation = false;
            show_NewPassport = true;
            show_ChangeOfNationality = false;
            show_Others = false;
        }
    }*/
    public void Render_NewDesignation()
    {
        system.debug('The Request Type Selected : ' + Application.request_type__c);
        if(Application.request_type__c == 'New Designation' && Application.request_type__c != 'Change Of Nationality' &&  Application.request_type__c != 'Others' && Application.request_type__c != 'New Passport')
        {
            show_NewDesignation = true;
            show_NewPassport = false;
            show_ChangeOfNationality = false;
            show_Others = false;
        }
        else if(Application.request_type__c == 'New Passport' && Application.request_type__c != 'New Designation' && Application.request_type__c != 'Change Of Nationality' &&  Application.request_type__c != 'Others')
        {
            show_NewDesignation = false;
            show_NewPassport = true;
            show_ChangeOfNationality = false;
            show_Others = false;
        }
        else if(Application.request_type__c == 'Lost Passport' && Application.request_type__c != 'New Designation' && Application.request_type__c != 'Change Of Nationality' &&  Application.request_type__c != 'Others' && Application.request_type__c != 'New Passport')
        {
            show_NewDesignation = false;
            show_NewPassport = true;
            show_ChangeOfNationality = false;
            show_Others = false;
        }
        else if(Application.request_type__c == 'Change Of Nationality' && Application.request_type__c != 'New Designation' && Application.request_type__c != 'Others' && Application.request_type__c != 'New Passport')
        {
            show_ChangeOfNationality = true;
            show_NewDesignation = false;
            show_NewPassport = false;
            show_Others = false;
        }
        else if(Application.request_type__c == 'Others' && Application.request_type__c != 'New Designation' && Application.request_type__c != 'Change Of Nationality' && Application.request_type__c != 'New Passport')
        {
            show_Others = true;
            show_ChangeOfNationality = false;
            show_NewDesignation = false;
            show_NewPassport = false;
        }
      /*  {
            show_NewDesignation = false;
            show_NewPassport = true;
            show_ChangeOfNationality = true;
            show_Others = true;
        }
    }
    public void Render_ChangeOfNationality()
    {
        if(Application.request_type__c == 'Change Of Nationality' && Application.request_type__c != 'New Designation' && Application.request_type__c != 'Others' && Application.request_type__c != 'New Passport')
        {
            show_ChangeOfNationality = true;
            show_NewDesignation = false;
            show_NewPassport = false;
            show_Others = false;
        }
        else
        {
            show_ChangeOfNationality = false;
            show_Others = true;
            show_NewDesignation = true;
            show_NewPassport = true;
        }
    }
    public void Render_Others()
    {
        if(Application.request_type__c == 'Others' && Application.request_type__c != 'New Designation' && Application.request_type__c != 'Change Of Nationality' && Application.request_type__c != 'New Passport')
        {
            show_Others = true;
            show_ChangeOfNationality = false;
            show_NewDesignation = false;
            show_NewPassport = false;
        }*/
        /*else
        {
            show_Others = false;
            show_ChangeOfNationality = true;
            show_NewDesignation = true;
            show_NewPassport = true;
        }*/
    }


    
    public List<SelectOption> getCompanies()
    {
        List<SelectOption> options = new List<SelectOption>();
        
        List<Account> CompaniesList = [SELECT id, Name FROM Account Order By Name];
        options.add(new SelectOption('', ''));
        for(Account company : CompaniesList)
        {
            options.add(new SelectOption(company.id, company.name));
        }
        
        return options;
    }
       public List<SelectOption> getReligions()
    {
        List<SelectOption> options = new List<SelectOption>();
        options.add(new SelectOption('', ''));
        options.add(new SelectOption('Bahaei', 'Bahaei'));
        options.add(new SelectOption('Budhist', 'Budhist'));
        options.add(new SelectOption('Cristian', 'Cristian'));
        options.add(new SelectOption('Hindu', 'Hindu'));
        options.add(new SelectOption('Kadiani', 'Kadiani'));
        options.add(new SelectOption('Muslim', 'Muslim'));
        options.add(new SelectOption('Sikh', 'Sikh'));
        
        return options;
    }

    public List<SelectOption> getMaritalStatuses()
    {
        List<SelectOption> options = new List<SelectOption>();
        
        options.add(new SelectOption('Single', 'Single'));
        options.add(new SelectOption('Married', 'Married'));
        options.add(new SelectOption('Divorced', 'Divorced'));
        options.add(new SelectOption('Widowed', 'Widowed'));
        
        return options;
    }
    
      public List<SelectOption> getCountries()
    {
        List<SelectOption> options = new List<SelectOption>();
        
        List<Country__c> CountriesList = [SELECT id, Name FROM Country__c WHERE IsActive__c = true Order By Name LIMIT 999];
        options.add(new SelectOption('', ''));
        for(Country__c country : CountriesList)
        {
            options.add(new SelectOption(country.id, country.name));
        }
        
        return options;
    }
    
        public List<SelectOption> getQualifications()
    {
        List<SelectOption> options = new List<SelectOption>();
        
        List<Qualification__c> QualificationsList = [SELECT id, Name FROM Qualification__c WHERE IsActive__c = true Order By Name LIMIT 999];
        options.add(new SelectOption('', ''));
        for(Qualification__c qualification : QualificationsList)
        {
            options.add(new SelectOption(qualification.id, qualification.name));
        }
        
        return options;
    }
   
    public List<SelectOption> getRequestTypes()
    {
        List<SelectOption> options = new List<SelectOption>();
        
        options.add(new SelectOption('New Passport', 'New Passport'));
        options.add(new SelectOption('New Designation', 'New Designation'));
        options.add(new SelectOption('Lost Passport', 'Lost Passport'));
        options.add(new SelectOption('Change Of Nationality', 'Change Of Nationality'));
        options.add(new SelectOption('Others', 'Others'));
        
        return options;
    }
    public List<SelectOption> getTitles()
    {
        List<SelectOption> options = new List<SelectOption>();
        
        options.add(new SelectOption('Mr', 'Mr'));
        options.add(new SelectOption('Mrs', 'Mrs'));
        options.add(new SelectOption('Ms', 'Ms'));
        
        return options;
    }
    
  
    Public PageReference ResetApplication()
    {
        Application = new Health_Card_Renewal_Form__c();
        
        return null;
    }
    
    Public PageReference ToMainMenu()
    {
        PageReference pg = new PageReference('/home');
        return pg;
    }
    public static Date stringToDate(String s){
      //XML Date String is in the format DD-MM-YYYY
      try{
          String[] stringDate = s.split('/');
          Integer d =  Integer.valueOf(stringDate[0]);
          Integer m = Integer.valueOf(stringDate[1]);
          Integer y = Integer.valueOf(stringDate[2]);
          
          if (d > 31 || m > 12 ||  y < 1800){
             ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Please make sure all Date values to be in DD-MM-YYYY format.'));
             return null ;
          }
          
          return date.newInstance(y,m,d);
      }catch(System.Exception er){
          ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Please make sure all Date values to be in DD-MM-YYYY format.'));
          return null ;
      }    
    }
    
    Public PageReference CreateApplication()
    {                       
        if (Application.request_type__c == null){
         ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Must Choose from (New Passport , New Designation , Lost Passport, Change Of Nationality)'));
         //return null ;
        }
        if (Application.request_type__c == 'Others'){
            if ( Application.Please_specify__c == null || Application.Please_specify__c == ''){
             ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Please specify "Others"'));
             //return null ;
            }
        }
        if (Application.request_type__c == 'Change Of Nationality'){
            if ( Application.Previous_Nationality__c == null){
             ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Must Choose Previous Nationality'));
             //return null ;
             }
            if ( Application.New_Nationality__c == null){
             ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Must Choose New Nationality'));
             //return null ;
             }
        }
        
        if (Application.request_type__c == 'New designation'){
           
           if ( Application.Previous_Job_Title_Position__c  == null){
              ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Must Choose Previous Job Title/Position'));
              //return null ;
           }
           
           if ( Application.Job_Title_Position__c == null){
              ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Must Choose Job Title/Position'));
              //return null;
          }
           
           
           if (Application.Passport_Number__c == '' || Application.Passport_Number__c == null){
              ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Must Insert Passport Number'));
              //return null;
           }
        }else if (Application.request_type__c == 'New Passport' || Application.request_type__c == 'Lost Passport'){
           if (Application.Job_Title_Position__c == null ){
              ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Must Choose Job Title/Position'));
              //return null;
           }
           
           if (Application.Religion__c == null){
           ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Must Choose Religion'));
           }
           if (Application.Marital_Status__c == null){
           ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Must Choose Marital Status'));
           }
           if (Application.Currently_Nationality__c == null ){
           ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Must Choose Currently Nationality'));
           }
           if (Application.Previous_Nationality__c == null ){
           ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Must Choose Previous Nationality'));
           }
           if (Application.Education_Qualification__c == null){
           ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Must Choose Education Qualification'));
           }
           if (Application.Mother_Full_Name__c == null || Application.Mother_Full_Name__c ==  ''){
           ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Must Insert Mother Full Name'));
           }
           if (Application.Emirates_dropdown__c == null){
           ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Must Insert Emirate'));
        }
        if (Application.City_dropdown__c == null){
           ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Must Insert City inside UAE'));
        }
        if (Application.P_O_Box__c == null || Application.P_O_Box__c == ''){
           ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Must Insert P.O.Box'));
        }
        if (Application.inside_residence_telephone__c == null || Application.inside_residence_telephone__c == ''){
           ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Must Insert Residence Telephone'));
        }
        if (Application.inside_office_telephone__c == null || Application.inside_office_telephone__c == ''){
           ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Must Insert Office Telephone'));
        }
         if(Application.Applicant_Mobile_Number__c == null || Application.Applicant_Mobile_Number__c ==  ''){
           ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Must Provide Applicant Mobile Number'));
        }
       
           if (Application.Passport_Number__c == '' || Application.Passport_Number__c == null){
              ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Must Insert Passport Number'));
              //return null;
           }
           if (Application.Previous_Passport_Number__c == '' || Application.Previous_Passport_Number__c == null){
              ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Must Insert Previous Passport Number'));
              //return null;
           }
        }
        
        
        if (Application.Title__c == null){
           ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Must Choose Title'));
           //return null ;        
        }
        if (Application.First_Name__c == 'First' || Application.First_Name__c == ''){
          ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Must enter a value for First Name'));
          //return null ;
        }
        if (Application.Middle_Name__c == 'Middle'){          
           Application.Middle_Name__c = '' ;
        }
        if (Application.Last_Name__c == 'Last' || Application.Last_Name__c == ''){
          ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Must enter a value for Last Name'));
          //return null ;
        }
        
        if (visaIssueDate == null || visaIssueDate ==  ''){
           ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Must Choose Visa Issue Date'));
           //return null ;
        }else{
           Application.Visa_Issue_Date__c = stringToDate(visaIssueDate);
        }
        
        
        if (Application.Company_Name__c == null){
           ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Must Choose a Company'));
           //return null ;
        }
        if (Application.Name_of_Authorized_Signatory__c == null || Application.Name_of_Authorized_Signatory__c == ''){
           ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Must Insert Name of Authorized Signatory'));
           //return null ;
        }
        if (companyDate == null || companyDate == ''){
           ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Must Choose Date'));
           //return null ;
        }else{
           Application.Date__c = stringToDate(companyDate);
        }
        
        
        if (ApexPages.hasMessages()){
           return null ;
        }
    
        try
        {
            insert(Application);
            
            PageReference pg = new PageReference('/apex/icnPDF?id=' + Application.id);
            return pg;
        }
        catch(System.Exception Exp)
        {
            ApexPages.AddMessages(Exp);
            return null;
        }
    }
   
   /* public static Date stringToDate(String s){
      //XML Date String is in the format dd/mm/yyyy
      String[] stringDate = s.split('-');
      Integer m =  Integer.valueOf(stringDate[0]);
      Integer d = Integer.valueOf(stringDate[1]);
      Integer y = Integer.valueOf(stringDate[2]);
      return date.newInstance(y,m,d);
    }*/
    
}